/*
 * @Author: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Date: 2025-08-13 18:02:04
 * @LastEditTime: 2025-10-11 15:46:47
 * @LastEditors: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Description: 
 * @FilePath: \lc_-esp32_-s3\main\main.c
 * 立创实战派ESP32-S3开发板
 * Copyright (c) 2025 by $4D Bios, All Rights Reserved. 
 */

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_log.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "key.h"
#include "console.h"
#include "bsp_nvs.h"
#include "bsp_spiffs.h"
#include "iic.h"
#include "qmi8658.h"
#include "pca9557.h"
#include "sensor_task.h"
#include "audio.h"

#include "blufi.h"
#include "test_sd_card.h"
#include "test_es7210.h"
#include "test_es8311.h"
#include "test_lcd.h"
#include "test_camera.h"
#include "test_lvgl.h"

#include "wifi_scan_connect.h"
#include "ble_hid_device.h"
#include "mp3_player.h"
#include "desktop_weather.h"
#include "handheld.h"

static const char* TAG = "main";

i2c_obj_t i2c0_master;

// 打印内存使用情况
void displayMemoryUsage() {
    size_t totalDRAM = heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
    size_t freeDRAM = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
    size_t usedDRAM = totalDRAM - freeDRAM;

    size_t totalPSRAM = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
    size_t freePSRAM = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
    size_t usedPSRAM = totalPSRAM - freePSRAM;

    size_t DRAM_largest_block = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
    size_t PSRAM_largest_block = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);

    float dramUsagePercentage = (float)usedDRAM / totalDRAM * 100;
    float psramUsagePercentage = (float)usedPSRAM / totalPSRAM * 100;

    ESP_LOGI(TAG, "DRAM Total: %zu bytes, Used: %zu bytes, Free: %zu bytes,  DRAM_Largest_block: %zu bytes", totalDRAM, usedDRAM, freeDRAM, DRAM_largest_block);
    ESP_LOGI(TAG, "DRAM Used: %.2f%%", dramUsagePercentage);
    ESP_LOGI(TAG, "PSRAM Total: %zu bytes, Used: %zu bytes, Free: %zu bytes, PSRAM_Largest_block: %zu bytes", totalPSRAM, usedPSRAM, freePSRAM, PSRAM_largest_block);
    ESP_LOGI(TAG, "PSRAM Used: %.2f%%\n", psramUsagePercentage);
} 

/**
 * @description: 主函数入口
 * @return {*}
 */
void app_main(void)
{
    //esp_log_level_set("*", ESP_LOG_ERROR); 
	//esp_log_level_set("*", ESP_LOG_INFO); 

    esp_log_level_set("sensor_task", ESP_LOG_ERROR); 

    nvs_init();
    // key_init();
    
    //console 会占用很多内部ram,这里先关闭该功能
    //console_init();

    i2c0_master = iic_init(I2C_NUM_0);

    pca9557_init(i2c0_master);   //初始化IO扩展芯片

    //sensor_task_init();          //获取各传感器数值任务

    /*  测试硬件和功能程序，验证完不用可屏蔽
    test_sd_card();
    test_es7210(i2c0_master);
    test_es8311(i2c0_master);
    test_lcd();
    test_camera(i2c0_master);
    test_lvgl(i2c0_master);
    */

    /* 蓝牙配网功能 */
    //blufi_net_config_init();

    bsp_lvgl_start(i2c0_master); //开启LVGL
    bsp_codec_init(i2c0_master);// 音频初始化

    /* 立创例程 只能单独一个一个运行Demo */
    //app_wifi_connect();
    //app_hid_ctrl();
    //mp3_player_init();
    //desktop_weather();
    handheld();
    
    while(1)
    {
        displayMemoryUsage();
        vTaskDelay(pdMS_TO_TICKS(30 * 1000));
    }
    
    vTaskDelay(10000 / portTICK_PERIOD_MS);
    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}
